package com.zb.kc.service.impl;

import com.zb.kc.commonutils.ResultCode;
import com.zb.kc.entity.EduVideo;
import com.zb.kc.dao.EduVideoDao;
import com.zb.kc.entity.form.VideoInfoForm;
import com.zb.kc.entity.vo.VideoVo;
import com.zb.kc.service.IEduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.kc.servicebase.exceptionhandler.KgcException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author wanggen
 * @since 2020-04-10
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoDao, EduVideo> implements IEduVideoService {

    @Autowired
    private EduVideoDao eduVideoDao;

    @Override
    public List<VideoVo> selectByCourseId(String courseId) {
        return eduVideoDao.selectByCourseId(courseId);
    }

    @Override
    public void saveVideoInfo(VideoInfoForm video) {
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(video, eduVideo);
        Integer isFree = video.getFree() ? 1 : 0;
        eduVideo.setIsFree(isFree);
        boolean save = this.save(eduVideo);
        if (!save) {
            throw new KgcException(ResultCode.VIDEO_ADD_FAIL);
        }
    }

    @Override
    public void updateVideo(VideoInfoForm video) {
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(video, eduVideo);
        Integer isFree = video.getFree() ? 1 : 0;
        eduVideo.setIsFree(isFree);
        boolean save = this.updateById(eduVideo);
        if (!save) {
            throw new KgcException(ResultCode.VIDEO_UPDATE_FAIL);
        }
    }

    @Override
    public VideoInfoForm getVideoFormById(String id) {
        return eduVideoDao.selectVideoInfoById(id);
    }
}
